package com.songcha.module_test.ui.view

import android.content.Context
import android.graphics.BlurMaskFilter
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import com.songcha.library_common.util.dp2px


class ShadowView
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0
) : FrameLayout(context,attrs,defStyle,defRes) {
    private lateinit var mShadowPaint:Paint
    private var mShadowColor:Int = Color.parseColor("#ffff0000")
    private var mBlurMaskFilterRadius:Float = 100f.dp2px()
    private var mShadowRadius:Float = 20f.dp2px()

    init {
        initView(context, attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?){
        setBackgroundColor(Color.TRANSPARENT)
        setLayerType(LAYER_TYPE_SOFTWARE, null)
        mShadowPaint = Paint()
        mShadowPaint!!.color = mShadowColor
        mShadowPaint!!.isAntiAlias = true
        //mShadowPaint!!.setShadowLayer(mShadowRadius,0f,0f,mShadowColor)
        mShadowPaint!!.maskFilter = BlurMaskFilter(mBlurMaskFilterRadius, BlurMaskFilter.Blur.NORMAL)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val rect = RectF().apply{
            left = 0f
            top = 0f
            right = width.toFloat()
            bottom = height.toFloat()
        }
        canvas.drawRect(rect,mShadowPaint)
        //canvas.drawRoundRect(rect,mShadowRadius,mShadowRadius,mShadowPaint)
    }

}